package com.dayuanit.dy16.ebook.ebookweb.service.impl;

import com.dayuanit.dy16.ebook.ebookweb.dto.CartDTO;
import com.dayuanit.dy16.ebook.ebookweb.entity.Cart;
import com.dayuanit.dy16.ebook.ebookweb.entity.Commodity;
import com.dayuanit.dy16.ebook.ebookweb.mapper.CommodityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseService {

    @Value("${ebook.picture.url}")
    protected String pictureUrl;

    @Autowired
    protected CommodityMapper commodityMapper;

    protected List<CartDTO> formatCartDTO(List<Cart> carts) {
        List<CartDTO> dtos = new ArrayList<>(carts.size());


        for (Cart cart : carts) {
            Commodity commodity = commodityMapper.selectByPrimaryKey(cart.getCommoId());

            CartDTO cartDTO = new CartDTO();
            dtos.add(cartDTO);
            cartDTO.setBuyNum(cart.getBuyNum());
            cartDTO.setCommoId(cart.getCommoId());
            cartDTO.setCommoName(commodity.getBookName());
            cartDTO.setPicUrl(pictureUrl + commodity.getPictureName());
            cartDTO.setPrice(commodity.getPrice());
            cartDTO.setCartId(cart.getId());
        }
        return dtos;
    }
}
